package com.chen.manager.config;

import com.chen.manager.interceptor.LoginAuthInterceptor;
import com.chen.manager.properties.UserAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author cgj
 * @date 2/8/25 7:51 AM
 */
@Component
public class WebMvcConfiguration implements WebMvcConfigurer {
    /**
     * 跨域处理和设置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")     //拦截的路径
                .allowCredentials(true)    //允许携带cookie
                .allowedHeaders("*")    // 允许携带头
                .allowedMethods("*")    //允许任意请求方式
                .allowedOriginPatterns("*");   //允许任意请求源
    }

    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;

    @Autowired
    private UserAuthProperties userAuthProperties;
    /**
     * 注册登录校验拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor).addPathPatterns("/**")
                .excludePathPatterns(userAuthProperties.getNoAuthUrls());
    }
}
